/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 28, 2012  3:20:39 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.promis.construction
 * File Name : PurchaseOrderServices.java
 * TODO
 */
package jpa.service.promis.construction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;

import org.apache.log4j.Logger;

import promis.utility.CommonService;
import jpa.entitys.common.CommonEntity;
import jpa.entitys.promis.construction.PurchaseOrder;
import jpa.entitys.promis.construction.PurchaseOrderBudget;
import jpa.entitys.promis.construction.PurchaseOrderItem;
import jpa.entitys.promis.construction.PurchaseOrderPK;
import jpa.service.common.JPAService;

/**
 * @author chatree
 *
 */
public class PurchaseOrderServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(PurchaseOrderServices.class);

	/**
	 * @param em
	 */
	public PurchaseOrderServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}
	
	
	public PurchaseOrder findById(PurchaseOrderPK id) throws Exception {
		
		return super.ConvertToYoureObject(super.find(id, "PurchaseOrder.findById"));				
	}

	public List<PurchaseOrder> findBySapPoNo1(String syncExternalRefNo) throws Exception {
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("syncExternalRefNo", syncExternalRefNo);
		//mapWhereValue.put("status", status);
		
		return super.ConvertToYoureObject(super.find("PurchaseOrder.findBySapPoNo1", mapWhereValue));
	}
	
	public PurchaseOrder InsertedOrUpdated(PurchaseOrder vlaue) throws Exception {
		PurchaseOrder obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getLastUpdOn()))
			{					
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.inserted(obj);
			}else{				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		return obj;
	}
	
	public PurchaseOrder Updated(PurchaseOrder vlaue) throws Exception {
		PurchaseOrder obj = vlaue;
		try{
			
			if(vlaue.getId() != null)
			{
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}				
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		return obj;
	}
	
	public List<PurchaseOrder> Inserted(List<PurchaseOrder> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(PurchaseOrder obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);
				}
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		return vlauelist;
	}
	

	public void Inserted(PurchaseOrder po, List<PurchaseOrderItem> poitem, List<PurchaseOrderBudget> poBudget) throws Exception {
		try{		
			List<CommonEntity> common_poitem = new ArrayList<CommonEntity>();	
			List<CommonEntity> common_poBudget = new ArrayList<CommonEntity>();
			
			if(!CommonService.IsNull(po)){
				
				po.setSyncCreatedby(this.getUser());
				po.setSyncCreatedon(this.getCurrent_date());
				po.setSyncUpdatedby(this.getUser());
				po.setSyncUpdatedon(this.getCurrent_date());
				
				if(!CommonService.IsNull(poitem) || !CommonService.IsNull(poBudget)){
					
					if(!CommonService.IsNull(poitem)){
						for(PurchaseOrderItem obj : poitem){
							obj.setSyncCreatedby(this.getUser());
							obj.setSyncCreatedon(this.getCurrent_date());
							obj.setSyncUpdatedby(this.getUser());
							obj.setSyncUpdatedon(this.getCurrent_date());
							common_poitem.add(obj);
						}
					}
					
					if(!CommonService.IsNull(poBudget)){
						
						for(PurchaseOrderBudget obj : poBudget){
							obj.setSyncCreatedby(this.getUser());
							obj.setSyncCreatedon(this.getCurrent_date());
							obj.setSyncUpdatedby(this.getUser());
							obj.setSyncUpdatedon(this.getCurrent_date());
							common_poBudget.add(obj);
						}
					}
				}
				
				if(!CommonService.IsNull(po))
					super.inserted(po, common_poBudget, common_poitem);
			}
			
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}		
	}
	
	public void Updated(PurchaseOrder po, List<PurchaseOrderItem> poitem, List<PurchaseOrderBudget> poBudget) throws Exception {
		try{		
			List<CommonEntity> common_poitem = new ArrayList<CommonEntity>();	
			List<CommonEntity> common_poBudget = new ArrayList<CommonEntity>();
			
			if(!CommonService.IsNull(po)){
				
				//po.setSyncCreatedby(this.getUser());
				//po.setSyncCreatedon(this.getCurrent_date());
				po.setSyncUpdatedby(this.getUser());
				po.setSyncUpdatedon(this.getCurrent_date());
				
				if(!CommonService.IsNull(poitem) || !CommonService.IsNull(poBudget)){
					
					if(!CommonService.IsNull(poitem)){
						for(PurchaseOrderItem obj : poitem){
							//obj.setSyncCreatedby(this.getUser());
							//obj.setSyncCreatedon(this.getCurrent_date());
							obj.setSyncUpdatedby(this.getUser());
							obj.setSyncUpdatedon(this.getCurrent_date());
							common_poitem.add(obj);
						}
					}
					
					if(!CommonService.IsNull(poBudget)){
						
						for(PurchaseOrderBudget obj : poBudget){
							//obj.setSyncCreatedby(this.getUser());
							//obj.setSyncCreatedon(this.getCurrent_date());
							obj.setSyncUpdatedby(this.getUser());
							obj.setSyncUpdatedon(this.getCurrent_date());
							common_poBudget.add(obj);
						}
					}
				}
				
				if(!CommonService.IsNull(po))
					super.updated(po, common_poBudget, common_poitem);
			}
			
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}		
	}
	
	public List<PurchaseOrder> Updated(List<PurchaseOrder> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();
			for(PurchaseOrder obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{					
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);					
				}
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		return vlauelist;
	}

}
